package com.xudm.imp.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.xudm.imp.entity.DataDict;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class PoiTemplateUtil {
    public void generateDocument() throws IOException {

        List<Map<String, Object>> dataList1 = new ArrayList<>();
        List<Map<String, Object>> dataList2 = new ArrayList<>();
        List<Map<String, Object>> dataList3 = new ArrayList<>();
        List<Map<String, Object>> dataList4 = new ArrayList<>();
        List<Map<String, Object>> dataList = new ArrayList<>();


        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<DataDict> dictList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            DataDict dataDict = new DataDict();
            dataDict.setFiledName("field" + i);
            dataDict.setAlias("alias" + i);
            dataDict.setType("type" + i);
            dataDict.setLen(i);
            dataDict.setEmpty("empty" + i);
            dataDict.setDefaultVal("");
            dictList.add(dataDict);
        }

        List<Map<String, Object>> loopList = new ArrayList<>();

        //表
        Map<String, Object> loopMap1 = new HashMap<String, Object>();
        loopMap1.put("dictList", dictList);
        loopMap1.put("type", "1");
        loopList.add(loopMap1);

        //json
        Map<String, Object> loopMap2 = new HashMap<>();
        loopMap2.put("picture", Pictures.ofLocal("C:\\Users\\19546\\Pictures\\微信图片_20240601014535.jpg").size(512, 512).create());
        loopMap2.put("type", "2");
        loopList.add(loopMap2);

        Map<String, Object> loopMap3 = new HashMap<>();
        loopMap3.put("dictList", dictList);
        loopMap3.put("type", "1");
        loopList.add(loopMap3);


        Map<String, Object> dataMap4 = new HashMap<>();
        dataMap4.put("title4", "title4xxxxxxx");
        dataMap4.put("loopList", loopList);
        dataList4.add(dataMap4);


        Map<String, Object> dataMap3 = new HashMap<>();
        dataMap3.put("title3", "title3xxxxxxx");
        dataMap3.put("dataList4", dataList4);

        dataList3.add(dataMap3);

        Map<String, Object> dataMap2 = new HashMap<>();
        dataMap2.put("title2", "title2xxxxxxx");
        dataMap2.put("dataList3", dataList3);

        dataList2.add(dataMap2);

        Map<String, Object> dataMap1 = new HashMap<>();
        dataMap1.put("title1", "title1xxxxxxx");
        dataMap1.put("dataList2", dataList2);

        dataList1.add(dataMap1);

        dataMap.put("dataList1", dataList1);

        dataList.add(dataMap);
        dataList.add(dataMap);

        Map<String, Object> rootMap = new HashMap<>();

        rootMap.put("dataList", dataList);

        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        Configure config = Configure.builder().useSpringEL()
                .bind("dictList", policy).build();

        String output = "D:\\devSpace\\DataSpace\\template\\output\\文档生成测试" + LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN) + ".docx";

        XWPFTemplate template = XWPFTemplate.compile("D:\\devSpace\\DataSpace\\template\\table_template.docx", config).render(rootMap);
        template.writeAndClose(new FileOutputStream(output));
        log.info("Document generated successfully,\n{}", output);
    }

    public static void main(String[] args) {
        PoiTemplateUtil poiTemplateUtil = new PoiTemplateUtil();
        try {
            poiTemplateUtil.generateDocument();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
